package walletadminlogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/common/utils"
	"time"

	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/key/walletkey"
	"navi-mumbai/common/xerr"

	"github.com/shopspring/decimal"
	"github.com/zeromicro/go-zero/core/logx"
)

type QueryUserWithdrawFlowListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryUserWithdrawFlowListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryUserWithdrawFlowListLogic {
	return &QueryUserWithdrawFlowListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 管理后台查询提现流水列表
func (l *QueryUserWithdrawFlowListLogic) QueryUserWithdrawFlowList(in *pb.QueryUserWithdrawFlowListReq) (*pb.QueryUserWithdrawFlowListResp, error) {
	defer utils.RecoverPanic()

	var start, end *time.Time
	if in.StartTime != "" {
		t, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.StartTime, time.Local)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
		}
		start = &t
	}
	if in.EndTime != "" {
		t, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.EndTime, time.Local)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
		}
		end = &t
	}
	var err error
	var uws []*wallet.UserWithdrawFlow
	uws, err = l.svcCtx.UserWithdrawFlowModel.Find(l.ctx, in.Uid, in.MoneyType, in.CurrencyType, in.PayChannelId, in.State, in.PageNo, in.PageSize, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	var rsp pb.QueryUserWithdrawFlowListResp
	rsp.List = make([]*pb.UserWithdrawFlow, 0)

	if len(uws) == 0 {
		return &rsp, nil
	}

	if in.PageNo <= 1 {
		rsp.Total, err = l.svcCtx.UserWithdrawFlowModel.Count(l.ctx, in.Uid, in.MoneyType, in.CurrencyType, in.PayChannelId, in.State, start, end)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
		}
	}

	for i := 0; i < len(uws); i++ {
		var vl pb.UserWithdrawFlow
		vl.FlowNo = uws[i].FlowNo
		vl.MoneyType = uws[i].MoneyType
		vl.CurrencyType = uws[i].CurrencyType
		vl.MoneyAmount = decimal.NewFromInt(uws[i].MoneyAmount).Div(decimal.NewFromInt(walletkey.MoneyScale)).StringFixedBank(2)
		vl.CurrencyAmount = decimal.NewFromInt(uws[i].CurrencyAmount).Div(decimal.NewFromInt(walletkey.MoneyScale)).StringFixedBank(2)
		vl.State = uws[i].State
		vl.CreateTime = uws[i].CreateTime.Format(dbkey.DateTimeCNFormat)
		l.Logger.Infof("UserWithdrawFlow.. AuditTime =====: %+v", uws[i].AuditTime)
		l.Logger.Infof("UserWithdrawFlow.. AuditTime2 =====: %+v", uws[i].AuditTime.Time.Format(dbkey.DateTimeCNFormat))
		l.Logger.Infof("UserWithdrawFlow.. AuditTime3 =====: %+v", uws[i].AuditTime.Time.Format(dbkey.DateTimeFormat3))
		if uws[i].AuditTime.Valid {
			vl.AuditTime = uws[i].AuditTime.Time.Format(dbkey.DateTimeCNFormat)
		}
		if uws[i].PushTime.Valid {
			vl.PushTime = uws[i].PushTime.Time.Format(dbkey.DateTimeCNFormat)
		}
		if uws[i].State == walletkey.WithdrawFlowStateSuccess {
			vl.PayTime = uws[i].UpdateTime.Format(dbkey.DateTimeCNFormat)
		}
		vl.PushMsg = uws[i].PushMsg
		vl.AuditMsg = uws[i].AuditMsg
		vl.Remark = uws[i].AuditMsg
		vl.AccountName = uws[i].AccountName
		vl.BankCardNumber = uws[i].BankCardNumber
		vl.BankName = uws[i].BankName
		vl.BankCode = uws[i].BankCode
		vl.AccountType = uws[i].AccountType
		vl.ServiceFeeRate = decimal.NewFromInt(uws[i].ServiceFeeRate).Div(decimal.NewFromInt(walletkey.MoneyScale)).StringFixedBank(2)
		vl.ServiceFeeAmount = decimal.NewFromInt(uws[i].ServiceFeeAmount).Div(decimal.NewFromInt(walletkey.MoneyScale)).StringFixedBank(2)
		rsp.List = append(rsp.List, &vl)
	}

	return &rsp, nil
}
